#include <bits/stdc++.h>
using namespace std;

#define FOR_INC(i, start, end) for (int i = start; i < end; ++i)
#define FOR_DEC(i, start, end) for (int i = start; i > end; --i)
#define FOR_INC_EQUAL(i, start, end) for (int i = start; i <= end; ++i)
#define FOR_DEC_EQUAL(i, start, end) for (int i = start; i >= end; --i)
#define int long long
#define lll __int128
class UnionFind{
public:
    vector<int> parent;
    int count;
    explicit UnionFind(int n){
        count = n;
        for(int i=0;i<n;i++){
            this->parent.emplace_back(i);
        }
    }

    void union1(int x,int y){
        int p = find(x);
        int q = find(y);
        if(p==q){
            return;
        }
        this->count--;
        this->parent[p] = q;
    }

    int find(int index){
        if(this->parent[index]!=index){
            this->parent[index] = find(this->parent[index]);
        }
        return index;
    }
};

class Fenwick {
    vector<int> f;

public:
    Fenwick(int n) : f(n) {}
    inline static int lowBit(int n){
        return n&-n;
    }
    void update(int i, int val) {
        for (; i < f.size(); i += lowBit(i)) {
            f[i] += val;
        }
    }

    int query(int i) {
        int res = 0;
        for (; i > 0; i -= lowBit(i)) {
            res += f[i];
        }
        return res;
    }

    void add(int i,int val){
        f[i]+=val;
    }

    int get(int index){
        return f[index];
    }
};

void getNums(const string& s){
    string t;
    for (char i : s) {
        if(i=='['){
            t+='{';
        }else if(i==']'){
            t+='}';
        }else{
            t+=i;
        }
    }
    cout<<t<<endl;
}

class Solution {
public:
    int findWinningPlayer(vector<int>& skills, int k) {
        int max = skills[0];
        int n = skills.size();
        int count = 0;
        int index = 0;
        for(int i=1;i<n;i++){
            if(max>skills[i]){
                count++;
            }else{
                max = skills[i];
                count=1;
                index = i;
            }
            if(count==k){
                break;
            }
        }
        return index;
    }

    long long sumDigitDifferences(vector<int>& nums) {
        long long ans = 1;
        unordered_map<int,unordered_set<int>> map1;
        for(int n:nums){
            int index = 0;
            int tmp = n;
            while(tmp!=0){
                map1[index++].insert(tmp%10);
                tmp/=10;
            }
        }
        auto iter = map1.begin();
        while(iter!=map1.end()){
            ans*=iter->second.size();
            iter++;
        }
        return ans == 1?0:ans;
    }
};

int fastpow(int base,int n,int mod){
    int ans = 1;
    while(n>0){
        if((n&1)==1){
            ans = (ans*base)%mod;
        }
        n>>=1;
        base = (base*base)%mod;
    }
    return ans;
}


signed main () {
//    vector<vector<int>> grid = {{0,0,1,1},{0,0,1,1},{0,2,1,3}};
//    vector<int> nums = {1,2,7};
//    Solution s;
//    vector<int> arr = {4,5,3};
//    s.minOperations(nums,arr);
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n,m;
    cin>>n>>m;
    vector<vector<int>> grid(n+1,vector<int>(m+1,0));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>grid[i][j];
            grid[i][j]+=grid[i][j-1];
        }
    }
    int ans = 0;
    for(int i=min(n,m);i>=1;i--){
        for(int j=1;j<=n;j++){
            int startI = j;
            int endI = startI+i-1;
            if(endI>n){
                break;
            }
            for(int k=1;k<=m;k++){
                int startJ = k;
                int endJ = startJ+i-1;
                if(endJ>m){
                    break;
                }
                bool flag = true;
                for(int p=startI;p<=endI;p++){
                    int value1 = grid[p][endJ]-grid[p][startJ-1];
                    int value2 = i;
                    if(value1!=value2){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    ans=i;
                    goto end;
                }
            }
        }
    }
    end:
    cout<<ans<<endl;
    return 0;
}
